#! /usr/bin/perl
use CGI qw(:all); # load standard CGI routines
use strict;
my $chartData = "[";
open (EQUITY_DATA , "</home/clyde/Desktop/forex-study/output/data/EQUITY_file_for_EURUSD.csv");
my $interval_temp = 5;  #show data by 5 interval
my $chartData = "[";
my $rowId = 0;
while(<EQUITY_DATA>){  
    chomp;
   $rowId++;
    if($rowId%5==1){
        my @rowData = split /\t/, $_;
        my $rd .= "[\"$rowData[1]\" , $rowData[2]],";
        $chartData .= $rd;
    }
}
$chartData .= "]";
close EQUITY_DATA;

my $tradesChartData = "[";
open (TRADES_DATA , "</home/clyde/Desktop/forex-study/output/data/TRADERECORD_file_for_EURUSD.csv");
while(<TRADES_DATA>){  
    chomp;
    my @rowData = split /\t/, $_;
    my $rd;
    if($rowData[2] eq "closePosition"){  
        if($rowData[7] > 0){
            $rd .= "[\"$rowData[1]\" , $rowData[7],$rowData[7],0,0],";
        }else{
            $rd .= "[\"$rowData[1]\" , 0,$rowData[7],0,$rowData[7]],";
        }        
        $tradesChartData .= $rd;        
    }
}
$tradesChartData .= "]";
close TRADES_DATA;

my $js = "

google.load('visualization', '1', {packages:['corechart']});
google.setOnLoadCallback(drawChart);
      function drawChart() {

// load the equite moving chart
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Date');
        data.addColumn('number', 'Equit(\$)');
        data.addRows($chartData); 

        var chart = new google.visualization.LineChart(document.getElementById('equity-chart'));
        chart.draw(data, {width: 1000, height: 500, title: 'Equity Performance'});

// load the trades P/L distribution chart

// Some raw data (not necessarily accurate)
         var data = google.visualization.arrayToDataTable($tradesChartData,true);

         // Create and draw the visualization.
        var candlestickChart = new google.visualization.CandlestickChart(document.getElementById('trades-PL-chart'));
        //candlestickChart.draw(dataTable, {legend:'none'});
        candlestickChart.draw(data, {
            title : 'Trades P/L Distribution Chart',
legend:'none',

            colors:['red','blue'], 
            width: 1000, height: 500
        });

}

";

print header(); # create the HTTP header



print start_html(-title=>'Performance for virtual trading test',
 -script=>[{-type=>'JAVASCRIPT', -src=>'https://www.google.com/jsapi'},
    $js]);
print h2 ("Equity-Chart:");
print div( {-id=>"equity-chart"}, "" );

print h2 ("Trades-PL-Chart:");
print div( {-id=>"trades-PL-chart"}, "" );


print end_html();

